//
//  LCBeautifulEssayController.m
//  JKFuYinProject
//
//  Created by jiukuyinyue on 2017/9/18.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCBeautifulEssayController.h"

///push跳转到登录界面 进行登录
#define pushAccLoginVC [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"AccLogin" bundle:nil] instantiateInitialViewController] animated:YES];

@interface LCBeautifulEssayController ()

@end

@implementation LCBeautifulEssayController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)setupWebView{
    // 根据生成的WKUserScript对象，初始化WKWebViewConfiguration
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
    self.webView.allowsBackForwardNavigationGestures = YES;//是否允许左右划手势导航,默认不允许
    
    // KVO 监听属性，具体参考 WKWebView 的头文件
    NSKeyValueObservingOptions observingOptions = NSKeyValueObservingOptionNew;
    [self.webView addObserver:self forKeyPath:@"title" options:observingOptions context:nil];
    
    [self.view addSubview:self.webView];
    self.webView.scrollView.bounces = NO;//禁止WebView滚动和回弹方法
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.link]];//goURLStrings[index]]];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];//创建NSURLRequest
    [self.webView loadRequest:request];//加载
    self.webView.alpha = 0;
    
    if (@available(iOS 11.0, *)) {
        self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    [self setupWebViewLayout];
}
- (void)setupWebViewLayout
{
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-0);
        }
    }];
}

- (void)setupWebViewCompatibilityADLayout
{
    CGFloat width = MIN(self.view.width , self.view.height);
    [self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-width/20*3);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-width/20*3);
        }
    }];
}

- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message{
        NSLog(@"JS 调用了 %@ 方法，传回参数 %@",message.name,message.body);
//    if([message.name isEqualToString:@"shop"])//此处name为JS传出信息打包的标志<name>
//    {}
}
#pragma mark -  网页加载前处理  -
/** 跳转处理 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:
(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    //[self getTitleWithUrl:url];
    if ([url containsString:@"http://buy.9ku.com/wap/goods"]) {
        if([[_UserM usercode] isEqualToString:@""]){
            [self gotoLogin];
        }else{
            LSZFindOutMoreVC *viewController = [[LSZFindOutMoreVC alloc] init];
            viewController.type = @"2";
            viewController.link = url;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        decisionHandler(WKNavigationActionPolicyCancel);//这个地方必须进行不跳转
        return;
    }
    if ([url containsString:@"https://itunes.apple.com/cn/app/"]){
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}
#pragma mark- 跳转登陆界面
- (void)gotoLogin {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"AccLogin" bundle:nil];
    UIViewController *controller = [sb instantiateInitialViewController];
    [self.navigationController pushViewController:controller animated:YES];
}
///加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    [super example1];
}

@end
